#include "blockqueue.hpp"
#include "Thread_mutex.hpp"

void funcofmain(TDataBlock* bd)
{
    int cnt = 5;
    while(cnt--)
    {
        cout << bd->_name << "--funcofmain" << endl;
        sleep(1);
    }
}

void funcofless(TDataBlock* bd)
{
    int cnt = 5;
    while(cnt--)
    {
        cout <<  bd->_name << "--funcofless" << endl;
        sleep(1);
    }
}

void CommFunc(vector<TYPE>& vtb, int pthreadnum, function<void(TDataBlock*)> func, string what, BlockQueue<DataType>* bq)
{
    for(int i=0; i<pthreadnum; i++)
    {     
        string thread_name = what + "-thread-" + to_string(i+1);

        TDataBlock* td = new TDataBlock(bq, thread_name);
        vtb.emplace_back(func, td);

        //??????
        vtb.back().CreateRun();
    }
}

void MainThread(vector<TYPE>& vtb, int pthreadnum, BlockQueue<DataType>* bq)
{
    CommFunc(vtb, pthreadnum, funcofmain, "main", bq);
}

void LessThread(vector<TYPE>& vtb, int pthreadnum, BlockQueue<DataType>* bq)
{
    CommFunc(vtb, pthreadnum, funcofless, "less", bq);
}

void WaitThread(vector<TYPE>& vtb)
{
    for(auto &e: vtb) e.Join();
}

int main()
{
    int a = 10;

    vector<TYPE> vtb;
    BlockQueue<DataType>* bq = new BlockQueue<DataType>(&a);

    MainThread(vtb, 1, bq);
    LessThread(vtb, 1, bq);
    WaitThread(vtb);

    return 0;
}